/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.manage.service;

import cn.hutool.core.bean.BeanUtil;
import info.peigen.hotpot.business.account.core.repository.entity.AccountEntity;
import info.peigen.hotpot.business.account.manage.convertor.AccountManageConvertor;
import info.peigen.hotpot.business.account.manage.form.AccountManageCreateForm;
import info.peigen.hotpot.business.account.manage.form.AccountManageEditForm;
import info.peigen.hotpot.business.account.manage.form.AccountManageSearchForm;
import info.peigen.hotpot.business.account.manage.info.AccountManageInfo;
import info.peigen.hotpot.business.account.manage.repository.service.AccountManageRepository;
import info.peigen.hotpot.common.core.result.PageResult;
import org.beetl.sql.core.query.LambdaQuery;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

/**
 * <b>(AccountManageService)</b>
 * 管理后台 "账户" 服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023-08-29
 */
@SuppressWarnings("unused")
@Component
public class AccountManageService {
    @Inject
    AccountManageRepository accountManageRepository;

    public PageResult<AccountManageInfo> page(AccountManageSearchForm form) {
        LambdaQuery<AccountEntity> lambdaQuery = accountManageRepository.getMapper().createLambdaQuery()
                .andLike(AccountEntity::getCustomerId, Query.filterLikeEmpty(form.getCustomerId(), false, true))
                .andLike(AccountEntity::getAccountId, Query.filterLikeEmpty(form.getAccountId(), false, true))
                .andLike(AccountEntity::getAccountType, Query.filterLikeEmpty(form.getAccountType(), false, true))
                .andLike(AccountEntity::getStatus, Query.filterLikeEmpty(form.getStatus(), false, true))
                .andLike(AccountEntity::getCurrency, Query.filterLikeEmpty(form.getCurrency(), false, true))
                .andLike(AccountEntity::getChannelCode, Query.filterLikeEmpty(form.getChannelCode(), false, true))
                .andLike(AccountEntity::getPartnerId, Query.filterLikeEmpty(form.getPartnerId(), false, true));

        return accountManageRepository.page(form.getPageNum(), form.getPageSize(), lambdaQuery, AccountManageConvertor::convertToInfo);
    }

    public void create(AccountManageCreateForm form) {
        AccountEntity entity = BeanUtil.copyProperties(form, AccountEntity.class);
        accountManageRepository.save(entity);
    }

    public void edit(AccountManageEditForm form) {
        AccountEntity entity = BeanUtil.copyProperties(form, AccountEntity.class);
        accountManageRepository.saveOrUpdate(entity);
    }

    public void remove(Integer id) {
        accountManageRepository.delete(id);
    }
}